package com.icss.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import com.icss.dao.OrderDao;
import com.icss.dao.OrderMessageBeanDao;
import com.icss.dao.OrderMessageDao;
import com.icss.dao.OrderStateDao;
import com.icss.entity.Address;
import com.icss.entity.Cart;
import com.icss.entity.CartBean;
import com.icss.entity.Custom;
import com.icss.entity.Order;
import com.icss.entity.OrderBean;
import com.icss.entity.OrderMessageBean;
import com.icss.entity.OrderState;

import sun.util.resources.cldr.ss.CurrencyNames_ss;


@Controller
public class OrderController {
	@Autowired
	private OrderDao oDao;
	@Autowired
	private OrderStateDao orderStateDao;
	@Autowired
	private OrderMessageBeanDao messagedao;
	@Autowired
	private OrderMessageDao mdao;


	/*第一部分*/
	@RequestMapping("logintest") // 登录判断
	public String fun(Integer c_id,HttpServletRequest request) {
		Custom custom = oDao.loginJudgement(c_id);
		HttpSession session = request.getSession();
		session.setAttribute("custom", custom);
		if (custom==null) {
			return "first";
		}
		else {
			return "home";
		}
	}



	/*第二部分:显示*/
	@RequestMapping("test") // 显示订单详情测试
	public String test(Model model,Integer oid) {
		System.out.println("already");
		List<OrderMessageBean> list = oDao.ShowOrderMessage(oid);
		for(OrderMessageBean attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getOMid());
		}
		model.addAttribute("ordermessage",list);
		return "test/test";
	}

	@RequestMapping("order") // 显示所有订单
	public String showOrder(Model model,Integer c_id) {
		System.out.println("001");		
		List<OrderBean> list = oDao.ShowAllOrders(c_id);
		for(OrderBean attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getOid());
		}
		model.addAttribute("order",list);
		return "myorder";
	}

	@RequestMapping("orderBS") // 根据订单状态查询订单
	public String showOrderByState(Model model,Integer c_id,Integer OSid) {
		System.out.println("001");
		List<OrderBean> list = oDao.ShowOrderByState(c_id, OSid);
		for(OrderBean attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getOSid());
		}
		model.addAttribute("order",list);
		return "myorder";
	}

	@RequestMapping("ordermessage") // 显示订单详情
	public String showOrderMessage(Model model,Integer oid) {
		System.out.println("already");
		List<OrderMessageBean> list = oDao.ShowOrderMessage(oid);
		for(OrderMessageBean attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getOMid());
		}
		model.addAttribute("ordermessage",list);
		return "ordermessage";
	}

	@RequestMapping("cart") // 显示订餐车
	public String showCart(Model model,Integer c_id) {
		System.out.println("already");
		List<CartBean> list = oDao.ShowCart(c_id);
		for(CartBean attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getCart_id());
		}
		model.addAttribute("cart",list);
		return "cart";
	}

	@RequestMapping("address") // 显示地址测试
	public String showAddress(Model model,Integer c_id) {
		System.out.println("address ready");
		List<Address> list = oDao.showAddress(c_id);
		for(Address attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getC_id());
		}
		model.addAttribute("address",list);
		System.out.println("address end");
		return "confirmorder";
	}

	@RequestMapping("showAll") // 显示新建订单的全部信息
	public String showAll(Model model1,Model model2,Model model3,Model model,Integer c_id,Integer Oid) {
		System.out.println("showAll is ready");
		System.out.println(Oid);
		List<Order> list1 = oDao.ShowTest(c_id, Oid);
		for(Order attribute : list1) {
			System.out.println(attribute);
			System.out.println(attribute.getOid());
			System.out.println(attribute.getC_id());
			System.out.println(attribute.getOamount());
		}
		List<OrderMessageBean> list2 = oDao.ShowOrderMessage(Oid);
		for(OrderMessageBean attribute : list2) {
			System.out.println(attribute);
			System.out.println(attribute.getDishName());
		}
		List<Address> list3 = oDao.showAddress(c_id);
		model1.addAttribute("order",list1);
		model2.addAttribute("ordermessage",list2);
		model3.addAttribute("address",list3);
		model.addAttribute("Oid", Oid);
		System.out.println("showAll is end");
		return "confirmorder";
	}

	@RequestMapping("showOM") // 显示订单的详细信息
	public String showOM(Model model1,Model model2,Integer Oid) {
		System.out.println("showOM is ready");
		System.out.println(Oid);
		List<OrderBean> list1 = oDao.OMorder(Oid);
		for(OrderBean attribute : list1) {
			System.out.println(attribute);
			System.out.println(attribute.getOid());
			System.out.println(attribute.getC_id());
			System.out.println(attribute.getOamount());
		}
		List<OrderMessageBean> list2 = oDao.OMmessage(Oid);
		for(OrderMessageBean attribute : list2) {
			System.out.println(attribute);
			System.out.println(attribute.getDishId());
		}
		model1.addAttribute("order",list1);
		model2.addAttribute("ordermessage",list2);
		System.out.println("showOM is end");
		return "ordermessage";
	}







	/*第三部分:删除*/
	@RequestMapping("deleteCart") // 删除订餐车信息
	public String deleteUser(Integer cart_id,Integer c_id) {		
		oDao.DeleteCart(cart_id);
		return "redirect:/cart.do?c_id="+c_id;
	}

	@RequestMapping("deleteAddress") // 删除地址
	public String deleteUser(Integer c_id,Integer Oid,Integer address_id) {		
		oDao.DeleteAddress(address_id);
		return "redirect:/showAll.do?c_id="+c_id+"&Oid="+Oid;
	}





	/*第三部分:修改*/
	@RequestMapping("editDNum") // 订餐车餐品数量修改
	public String editMNumber(Integer cart_id,Integer dish_num,Integer c_id) {
		oDao.editDNum(cart_id,dish_num);
		return "redirect:/cart.do?c_id="+c_id;
	}


	@RequestMapping("editAddress") // 收货地址修改
	public String editAddress(Integer address_id,Integer c_id, Integer Oid,Integer address_phone, String address_name,String address_detail) {
		oDao.editAddress(address_id, address_name, address_phone, address_detail);
		return "redirect:/showAll.do?c_id="+c_id+"&Oid="+Oid;
	}

	@RequestMapping("editOrder") // 订单修改
	public String editOrder(Integer address_id,Integer Oid) {
		oDao.editOrder(Oid, address_id);
		return "checkout";
	}

	/*第四部分:添加*/
	@RequestMapping("addCart") // 添加订餐车
	public String addCart( Integer DishId, Integer cart_id,HttpServletRequest request)
			throws Exception {
		HttpSession session=request.getSession();
		Custom custom=(Custom) session.getAttribute("custom");
		int c_id=custom.getC_id();			
		System.out.println(custom.getC_id()+"---"+DishId+"--"+c_id);
		oDao.insertCart(c_id,cart_id,DishId);
		return "redirect:/cart.do?c_id="+c_id;
	}
	@RequestMapping("addAddress") // 添加地址
	public String addAddress(Model model,Address address,Integer address_id, Integer c_id, Integer Oid,Integer address_phone, String address_name,String address_detail)
			throws Exception {
		oDao.insertAddress(address);
		//		List<Address> list = oDao.showAddresstest(c_id,address_id);
		//		model.addAttribute("addrtest",list);
		//		model.addAttribute("big",Oid);
		System.out.println(Oid);
		return "redirect:/showAll.do?c_id="+c_id+"&Oid="+Oid;
	}

	@RequestMapping("addMessage") // 添加留言
	public String addMessage(Integer Oid,Integer c_id,String message)
			throws Exception {
		oDao.editMessage(Oid, message);
		System.out.println(message);
		return "redirect:/showAll.do?c_id="+c_id+"&Oid="+Oid;
	}

	@RequestMapping("addOrdertest") // 添加订单测试
	public String addOrdertest(Model model,Integer c_id, Integer Oid, Integer Oamount, Order order)
			throws Exception {
		System.out.println("it can start");
		oDao.insertOrder(order);
		List<Order> list = oDao.ShowTest(c_id, Oid);
		for(Order attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getOid());
			System.out.println(attribute.getC_id());
			System.out.println(attribute.getOamount());
		}
		model.addAttribute("ordertest",list);
		return "test/confirmordertest";
	}


	@RequestMapping("addOrder") // 添加订单、批量添加订单详情、删除订餐车
	public String addOrder(Integer c_id,Integer Oid, Integer Oamount,Order order) {
		System.out.println(Oid);
		oDao.insertOrder(order);  //生成订单
		List<Cart> list = oDao.useCart(c_id);
		for(Cart attribute : list) {
			System.out.println(attribute);
			System.out.println(attribute.getCart_id());
			Integer cart_id=attribute.getCart_id();
			Integer DishId=attribute.getDishId();
			Integer dish_num=attribute.getDish_num();
			oDao.insertOrderMessage( cart_id,DishId,Oid,dish_num);    //订餐车信息加入订单详情
			oDao.DeleteCart(cart_id);                                 //删除订餐车内容
		}
		return "redirect:/showAll.do?c_id="+c_id+"&Oid="+Oid;
	}



	/*第五部分:跳转*/
	@RequestMapping("turnEdit") // 修改地址跳转
	public String turnEdit(Model model,Integer Oid,Address address) {
		System.out.println(address.getAddress_name());
		model.addAttribute("address", address);
		model.addAttribute("Oid",Oid);
		return "editaddress";
	}

	@RequestMapping("turnAdd") // 添加地址跳转
	public String turnAdd(Model model,Integer Oid) {
		model.addAttribute("Oid",Oid);
		return "address";
	}
	
	@RequestMapping("turnMessage") // 添加地址跳转
	public String turnMessage(Model model,Integer Oid) {
		model.addAttribute("Oid",Oid);
		return "addmessage";
	}


	/*其他部分*/
	@RequestMapping("orderAll")
	public String orderAll(Order order,Model model) {
		List<OrderBean> beans=oDao.findAll(order);
		List<OrderState> list=orderStateDao.findStateAll();
		model.addAttribute("orderManage",beans);
		model.addAttribute("orderState",list);
		return "background/orderManage";
	}
	@RequestMapping("orderDele") 
	public String deleOrder(Integer Oid) {
		System.out.println("删除"+Oid);
		int r=oDao.DeleteCord(Oid);
		if (r>0) {
			return "redirect:/orderAll.do";
		}
		else {	
			return "background/orderManage";
		}
	}
	@RequestMapping("orderMessage") 
	public String orderMessage(Integer Oid,Model model) {
		System.out.println("查看"+Oid);
		List<OrderMessageBean> list=messagedao.findByOid(Oid);
		OrderBean bean=oDao.findAddByOid(Oid);
		model.addAttribute("orderMessage",list);
		model.addAttribute("orderAdd",bean);
		return "background/orderMessage";
	}
	@RequestMapping("orderEdit") 
	public String orderEdit(Integer Oid,Model model,HttpServletRequest request) {
		System.out.println("修改"+Oid);
		request.getSession().setAttribute("oid", Oid);
		List<OrderMessageBean> list=messagedao.findByOid(Oid);
		List<OrderState> stlist=orderStateDao.findStateAll();
		Order order=oDao.findByOid(Oid);
		model.addAttribute("orderMessage",list);
		model.addAttribute("orderState",stlist);
		model.addAttribute("order",order);
		return "background/orderEdit";
	}
	@RequestMapping("oamountEdit") 
	public String oamountEdit(Integer Oid) {
		System.out.println("修改金额"+Oid);
		int r=oDao.updateOa(Oid, Oid);
		if (r>0) {
			return "redirect:/orderAll.do";
		}else {
			return "redirect:/orderMessage.do";
		}
	}
	@RequestMapping("editOmDn") 
	public String editOmDn(Integer OMid,Integer Mnumber,Integer Oid) {
		System.out.println("修改数量"+OMid);
		int r=mdao.editOmNum(OMid, Mnumber);
		if (r>0) {
			return "redirect:/oamountEdit.do?Oid="+Oid;
		}
		else {
			return "redirect:/orderEdit.do";
		}
	}
	@RequestMapping("upOs") 
	public String upOs(Integer OSid,HttpServletRequest request) {
		System.out.println("修改状态"+OSid);
		Integer Oid=(Integer) request.getSession().getAttribute("oid");
		int r=oDao.upOs(OSid, Oid);
		if (r>0) {
			return "redirect:/oamountEdit.do?Oid="+Oid;
		}
		else {
			return "redirect:/orderEdit.do";
		}
	}


}
